package com.mspbots.core.teams;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.teams.model.TeamsUserMessage;
import com.mspbots.core.teams.service.TeamsUserMessageService;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.concurrent.Callable;

/**
 * com.mspbots.core.teams.TeamsUserMessageController
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/9/16
 */
@Log4j2
@RestController
@RequestMapping(path = "/teams/user-message")
public class TeamsUserMessageController extends AbstractBaseController<TeamsUserMessage, TeamsUserMessageService> {

    @Override
    @GetMapping("page")
    public Callable selectPage(Page<TeamsUserMessage> page, TeamsUserMessage queryParams) {
        return () -> this.service.selectPage(page, queryParams);
    }

    @Override
    @PostMapping
    public Callable insert(@RequestBody TeamsUserMessage entity) {
        Assert.notNull(entity, () -> "wrong parameter");
        return () -> this.service.saveOnUser(entity);
    }

    @PostMapping("/{message}")
    public Callable senderMessage(@PathVariable("message") String message, @RequestBody SenderMessageBody body) {
        Assert.notNull(body.getTeamsUserId(), "teamsUserId is can not be empty or null.");
        log.info("TeamsUserMessageController Teams senderMessage body {}", body);
        return () -> this.service.messageHandlers(message, body);
    }

    @GetMapping("/check/before-out")
    public Callable checkBeforeOut(SenderMessageBody param) {
        Assert.notNull(param.getTeamsUserId(), "teamsUserId is can not be empty or null.");
        log.info("TeamsUserMessageController Teams checkBeforeOut body {}", param);
        return () -> this.service.checkUtilizationBeforeOut(param.teamsUserId, param.time);
    }


    @GetMapping("/check/timeEntryDuration")
    public Callable checkBeforeOut() {
        return () -> this.service.getTimeEntryDurationTest(null);
    }


    @GetMapping(path = "/list-date")
    public Callable selectListByDate(TeamsUserMessage q) {
        return () -> service.list(Wrappers.<TeamsUserMessage>lambdaQuery()
                .eq(TeamsUserMessage::getTenantId, q.getTenantId())
                .ge(ObjectUtils.isNotEmpty(q.getCreateDate()), TeamsUserMessage::getCreateDate, q.getCreateDate()));
    }

    @Data
    @NoArgsConstructor
    public static class SenderMessageBody {
        private String teamsUserId;
        private String reason;
        private String time;
        private LocalDateTime logTime;
        private Integer duration;
        private int fromType;
        private Boolean check;

        @Builder
        public SenderMessageBody(String teamsUserId, String reason, String time, LocalDateTime logTime, Integer duration, Boolean check) {
            this.teamsUserId = teamsUserId;
            this.reason = reason;
            this.time = time;
            this.logTime = logTime;
            this.duration = duration;
            this.check = check;
        }
    }
}
